package com.cwj.mansysback.servlet.impl;

import com.cwj.mansysback.entity.Account;
import com.cwj.mansysback.entity.LocalAccount;
import com.cwj.mansysback.service.api.AccountService;
import com.cwj.mansysback.service.impl.AccountServiceImpl;
import com.cwj.mansysback.servlet.filter.LockFilter;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class PwdServlet extends HttpServlet {

    AccountService accSer = new AccountServiceImpl();

    private final static String INITPWD = "123456";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        LocalAccount loAcc = LockFilter.localAccount;

        String aid = loAcc.getAid();
        Integer accrank =loAcc.getAccrank();

        Statement statement = new Statement(8);

        if(req.getRequestURI().contains("/Lock/Pwd/Change")){
            String oldPwd = req.getParameter("oldPwd");
            String newPwd = req.getParameter("newPwd");
            Account acc = new Account(aid,oldPwd,accrank);
            if(accSer.isTrue(acc)){
                if(accSer.setPassword(aid,accrank,newPwd))  statement.setStatue(0);
            }else{
                statement.setStatue(1);
            }

        }else if(req.getRequestURI().contains("/Lock/Pwd/Reset")){
            String tmpAid = req.getParameter("aid");
            String tmpRank = req.getParameter("accrank");
            if(tmpRank != null && tmpAid != null){
                if(accSer.setPassword(tmpAid,Integer.valueOf(tmpRank),INITPWD))  statement.setStatue(0);
            }

        }

        MyUtils.writeResponse(resp,statement);


    }
}
